package com.zw.pm.module.board.dailyoutputtotal;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Description: 日产量总计 控制层
 * @Author: China.wsg
 * @Date: 2020/01/09 08:42
 */
@Controller
@RequestMapping("/board/dailyTotal")
public class DailyOutputTotalController extends BaseController {

    @Autowired
    private DailyOutputTotalService dailyOutputTotalService;

    /**
     * 日产量总计导入 页面
     *
     * @return 页面路径
     */
    @GetMapping
    @RequiresPermissions({"board:dailyTotal"})
    public String dailyTotal(Model model) {
        model.addAttribute("date", new Date());
        return "board/dailyTotal/page";
    }

    /**
     * 导入日报
     *
     * @param file      导入文件
     * @param yearMonth 日报所属年月
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/import")
    @RequiresPermissions({"board:dailyTotal"})
    public JsonResult dailyTotalImport(@RequestParam("file") MultipartFile file, @RequestParam("yearMonth") Date yearMonth, HttpServletResponse response) {
        String msg = dailyOutputTotalService.dailyTotalImport(file, yearMonth);
        JsonResult jsonResult;

        if ("导入失败".equals(msg)) {
            jsonResult = error();
        }else {
            jsonResult = success();
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 判断文件是否存在
     *
     * @param url 文件路径
     * @return
     */
    @ResponseBody
    @PostMapping("/exist")
    public JsonResult exist(String url) {
        return jsonResult(dailyOutputTotalService.exist(url));
    }


}
